package com.huitone.gddw.service.impl.worktable;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.RedUserList;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IRedUserListDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.IRedUserListService;
import com.huitone.gddw.service.dto.RedUserListDTO;
import com.huitone.gddw.utils.StringUtils;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 红名单用户 接口实现类
 * @author Weiliyu
 *
 */
@Service
public class RedUserListService implements IRedUserListService {
	
	@Autowired
	private IRedUserListDao redUseDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;

	@Override
	public QueryPage<RedUserListDTO> selectPage(QueryPage<RedUserList> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("svrNode", page.getParams().get("svrNode"));
		map.put("phone", page.getParams().get("phone"));
		map.put("status", page.getParams().get("status"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<RedUserListDTO> result = redUseDao.selectPage(map);
		int totalCount = redUseDao.selectCount(map);
		
		QueryPage<RedUserListDTO> queryPage = new QueryPage<RedUserListDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public RedUserList selectByPrimaryKey(String recId) {
		return redUseDao.selectByPrimaryKey(recId);
	}

	@Override
	public RedUserListDTO toUpdate(String recId) {
		return new RedUserListDTO(this.selectByPrimaryKey(recId));
	}

	@Override
	public void changeStatus(RedUserList entity) {
		this.update(entity);
	}

	@Override
	public void save(RedUserList entity) {
		// TODO Auto-generated method stub
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		redUseDao.insertSelective(entity);
		this.updateParamRec("保存红名单用户");
	}

	@Override
	public void update(RedUserList entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		redUseDao.updateByPrimaryKey(entity);
		this.updateParamRec("更新红名单用户");
	}

	@Override
	public void delete(String recId) {
		redUseDao.deleteByPrimaryKey(recId);
		
		this.updateParamRec("删除红名单用户");
	}

	@Override
	public List<RedUserListDTO> getRecord(Map<String, Object> params) {
		List<RedUserListDTO> list = redUseDao.getRecord(params);
		return list;
	}

	@Override
	public int saveBatch(List<RedUserList> list) {
		for(RedUserList redUse : list){
			redUse.setRecId(StringUtils.uuid());
			redUse.setInsertTime(new Date());
			redUse.setInsertStaffId(SysLoginUtil.getLoginUserId());
			redUse.setLastModStaffId(redUse.getInsertStaffId());
			redUse.setLastModTime(redUse.getInsertTime());
		}
		if (!list.isEmpty() && list.size() >0) {
			this.updateParamRec("批量保存红名单用户");
			return redUseDao.insertbatch(list);
		}
		return 0;
	}

	@Override
	public void delAllByFileIds(List<String> list) {
		if (!list.isEmpty() && list.size() > 0) {
			redUseDao.delAllByFileIds(list);
		}
		this.updateParamRec("根据多个文件ID删除红名单用户");
	}

	@Override
	public void changeStatusByFileIds(String[] fileIds, String status) {
		List<String> list = Arrays.asList(fileIds);
		if (!list.isEmpty() && list.size() > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("status", status);
			map.put("list", list);
			redUseDao.changeStatus(map);
		}
		
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.REDUSE, desc);
	}

	@Override
	public List<RedUserList> selectByphone(String phone) {
		return redUseDao.selectByphone(phone);
	}
}
